package com.pickup.netty.server.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

public class WSSServerInitializer extends ChannelInitializer<SocketChannel> {


	@Override
	protected void initChannel(SocketChannel ch) throws Exception {

		ChannelPipeline pipeline = ch.pipeline();

		//pipeline.addLast(new ProtocolFrameDecoder());

//        // 粘包拆包
//        pipeline.addLast(new LengthFieldBasedFrameDecoder(1024*1024*1024, 0, 4, 0, 4));
//
//        pipeline.addLast(new ProtocolFrameDecoder());
//
//        // 解码器
//        pipeline.addLast(MESSAGE_CODEC);

		//pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));

		// HttpRequestDecoder和HttpResponseEncoder的一个组合，针对http协议进行编解码






		pipeline.addLast(new HttpServerCodec());

		pipeline.addLast(new ChunkedWriteHandler());

		// webSocket 数据压缩扩展，当添加这个的时候WebSocketServerProtocolHandler的第三个参数需要设置成true
		// pipeline.addLast(new WebSocketServerCompressionHandler());

		pipeline.addLast(new HttpObjectAggregator(1024*1024*64));

		pipeline.addLast(new IdleStateHandler(8, 10, 12));

		pipeline.addLast(new HeartBeatHandler());

		pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

		pipeline.addLast(new ChatInboundHandler());

		//pipeline.addLast(new TestHandler());
	}



}
